-- gain_pet
-- Created by chengb Sep/11/2015
-- 成就：获得冈布奥

-- 内部函数声明
local whenGainPet;

-- 注册完成获得冈布奥的回调
EventMgr.register("game_achievement/gain_pet", event.GAIN_PET, function(args)
    whenGainPet(args);
end);

-- 注册完成获得冈布奥的回调
whenGainPet = function(classId)
    -- 判断是否是符合要求的成就
    local function isValidId(id)
        if true == GameAchievementM.isCompleted(id) then
            -- 已经完成了
            return false;
        end

        local config = GameAchievementM.query(id);
        local ruleArg = config["rule_arg"];
        if type(ruleArg) == 'table' then
            if type(ruleArg["class_id"]) == 'number' and
               ruleArg["class_id"] ~= classId then
                -- 不是需求的获得冈布奥
                return false;
            end
        end

        return true;
    end

    -- 遍历所有的子规则
    local ids = GameAchievementM.getIdsByRule("gain_pet");
    for _, id in pairs(ids) do
        if isValidId(id) then
            -- 尝试完成成就
            GameAchievementM.tryComplete(id, 1);
        end
    end
end

return {
    -- 获取成就描述
    queryDesc = function(id)
        local config = GameAchievementM.query(id);
        local ruleArg  = config["rule_arg"];

        return string.gsub(config["desc"], "{pet}", PetM.query(ruleArg.class_id, "name"));
    end,
};